package pentago.engine;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;

public class RotationArrow extends ClickableObject {

    private static RotationArrow CLICKED_ARROW = null;
    private static byte TURNED_DIRECTION = Move.UNKNOWN_ROTATION_DIRECTION;
    private static boolean coloringEnabled = true;

    private byte direction;
    private byte assignedPlate;
    // private float scale;
    private Color clickedColor = Color.blue;
    private Color notClickedColor = Color.red;
    private boolean clicked = false;

    public static void resetHighlight() {
	TURNED_DIRECTION = Move.UNKNOWN_ROTATION_DIRECTION;
	CLICKED_ARROW.clicked = false;
	CLICKED_ARROW = null;
    }

    public static void disableColoring() {
	coloringEnabled = false;
    }

    public static void enableColoring() {
	coloringEnabled = true;
    }

    public RotationArrow(GameEngine gameEngine, int xPos, int yPos, Image img,
	    float scale, byte assignedPlate, byte direction) {
	super(gameEngine, xPos, yPos, img);
	this.assignedPlate = assignedPlate;
	this.direction = direction;
	// this.scale = scale;
	this.width *= scale;
	this.height *= scale;
	rotateArrow();
    }

    @Override
    public void executeClick(int xPos, int yPos, int button) {
	if (button != Input.MOUSE_LEFT_BUTTON)
	    return;

	if (!isClicked(xPos, yPos))
	    return;

	gameEngine.rotationArrowClicked(assignedPlate, direction);

	if (!coloringEnabled)
	    return;

	if (CLICKED_ARROW != null) {
	    if (TURNED_DIRECTION == Move.UNKNOWN_ROTATION_DIRECTION) {
		CLICKED_ARROW = this;
		clicked = true;
		TURNED_DIRECTION = direction;
	    } else {
		if (this.assignedPlate == CLICKED_ARROW.assignedPlate
			&& this.direction != CLICKED_ARROW.direction) {
		    CLICKED_ARROW.clicked = false;
		    CLICKED_ARROW = null;
		    TURNED_DIRECTION = Move.UNKNOWN_ROTATION_DIRECTION;
		}
	    }
	} else {
	    CLICKED_ARROW = this;
	    TURNED_DIRECTION = direction;
	    clicked = true;
	}
    }

    public byte getDirection() {
	return direction;
    }

    public byte getAssignedPlate() {
	return assignedPlate;
    }

    public int getX() {
	int returnedX = this.xPos;
	int shift = (this.height - this.width) / 2;

	switch (assignedPlate) {
	case GameBoard.UPPER_LEFT:
	    if (direction == Move.CLOCKWISE) {
		returnedX -= shift;
	    }
	    break;
	case GameBoard.UPPER_RIGHT:
	    if (direction == Move.COUNTERCLOCKWISE) {
		returnedX -= shift;
	    }
	    break;
	case GameBoard.LOWER_LEFT:
	    if (direction == Move.COUNTERCLOCKWISE) {
		returnedX -= shift;
	    }
	    break;
	case GameBoard.LOWER_RIGHT:
	    if (direction == Move.CLOCKWISE) {
		returnedX -= shift;
	    }
	    break;
	}
	return returnedX;
    }

    public int getY() {

	int returnedY = this.yPos;
	int shift = (this.height - this.width) / 2;

	switch (assignedPlate) {
	case GameBoard.UPPER_LEFT:
	    if (direction == Move.CLOCKWISE) {
		returnedY += shift;
	    }
	    break;
	case GameBoard.UPPER_RIGHT:
	    if (direction == Move.COUNTERCLOCKWISE) {
		returnedY += shift;
	    }
	    break;
	case GameBoard.LOWER_LEFT:
	    if (direction == Move.COUNTERCLOCKWISE) {
		returnedY += shift;
	    }
	    break;
	case GameBoard.LOWER_RIGHT:
	    if (direction == Move.CLOCKWISE) {
		returnedY += shift;
	    }
	    break;
	}
	return returnedY;
    }

    public Color getColor() {
	return clicked ? clickedColor : notClickedColor;
    }

    public void setClickedColor(Color clickedColor) {
	if (clickedColor == notClickedColor)
	    return;
	this.clickedColor = clickedColor;
    }

    public void setNotClickedColor(Color notClickedColor) {
	if (notClickedColor == clickedColor)
	    return;
	this.notClickedColor = notClickedColor;
    }

    private void rotateArrow() {

	float rotation = 0;

	switch (assignedPlate) {
	case GameBoard.UPPER_LEFT:
	    if (direction == Move.CLOCKWISE) {
		rotation = -90;
		this.width ^= this.height;
		this.height ^= this.width;
		this.width ^= this.height;
	    }
	    break;
	case GameBoard.UPPER_RIGHT:
	    if (direction == Move.COUNTERCLOCKWISE) {
		this.width ^= this.height;
		this.height ^= this.width;
		this.width ^= this.height;
		rotation = 90;
	    }
	    break;
	case GameBoard.LOWER_LEFT:
	    if (direction == Move.CLOCKWISE) {
		rotation = 180;
	    } else {
		this.width ^= this.height;
		this.height ^= this.width;
		this.width ^= this.height;
		rotation = -90;
	    }
	    break;
	case GameBoard.LOWER_RIGHT:
	    if (direction == Move.CLOCKWISE) {
		this.width ^= this.height;
		this.height ^= this.width;
		this.width ^= this.height;
		rotation = 90;
	    } else {
		rotation = 180;
	    }
	    break;
	default:
	    break;
	}

	img.rotate(rotation);
    }
}
